﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MaxStudio.Concrete.Web.RPT;
using System.Data;
using System.Data.SqlClient;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class ViewScrapConcreteReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            BindReport("2012-9-1");
            // default 42 have records.
        }

        private void BindReport(string strInputDate)
        {
            RPTScrapConcreteService RPTScrapConcreteServiceObj = ServiceFactory.getRPTScrapConcreteService();
            OperationResult<List<tb_RPT_ScrapConcrete>> resultList = RPTScrapConcreteServiceObj.GetScrapConcreteByInputDate(strInputDate);
            List<tb_RPT_ScrapConcrete> list = resultList.obj;

            DataSet ds = new DataSet();
            DataTable dt = ds.Tables.Add("tb_RPT_ScrapConcrete");
            dt.Columns.Add("Id");
            dt.Columns.Add("Weather");
            dt.Columns.Add("InputDate");
            dt.Columns.Add("ProjectName");
            dt.Columns.Add("ConstructionUnit");
            dt.Columns.Add("ConstructionSite");
            dt.Columns.Add("IntensityLevel");
            dt.Columns.Add("DevelopmentTime");
            dt.Columns.Add("Reason");
            dt.Columns.Add("Quantity");
            dt.Columns.Add("Cement");
            dt.Columns.Add("ProductLine");
            dt.Columns.Add("ApproachTo");
            dt.Columns.Add("CarryOn");
            dt.Columns.Add("CreateBy");
            dt.Columns.Add("CreateDate");
            dt.Columns.Add("ModifyBy");
            dt.Columns.Add("ModifyDate");
            dt.Columns.Add("IsDelete");

            foreach (tb_RPT_ScrapConcrete entity in list)
            {
                dt.Rows.Add(entity.Id
                    , entity.Weather
                    , entity.InputDate
                    , entity.ProjectName
                    , entity.ConstructionUnit
                    , entity.ConstructionSite
                    , entity.IntensityLevel
                    , entity.DevelopmentTime
                    , entity.Reason
                    , entity.Quantity
                    , entity.Cement
                    , entity.ProductLine
                    , entity.ApproachTo
                    , entity.CarryOn
                    , entity.CreateDate
                    , entity.CreateBy
                    , entity.ModifyBy
                    , entity.ModifyDate
                    , entity.IsDelete);

            }

            TableLogOnInfo logOnInfo = new TableLogOnInfo();
            ReportDocument oRpt = new ReportDocument();
            string path;
            path = Server.MapPath("/RPT/oCRTScrapConcrete.rpt");
            oRpt.Load(path);

            ParameterFields paramFields = new ParameterFields();
            ParameterDiscreteValue discreteVal = new ParameterDiscreteValue();
            ParameterField paramField = new ParameterField();

            //paramField.ParameterFieldName = "PDeviceName";
            //discreteVal.Value = strDeviceName;
            //paramField.CurrentValues.Add(discreteVal);
            //paramFields.Add(paramField);

            //从web.config中获取logOnInfo参数信息
            string ReportServerName, ReportDataBase, ReportUserName, ReportPassWord;
            //获取ServerName
            ReportServerName = ConfigurationManager.AppSettings["ReportServerName"].ToString();
            //获取DatabaseName
            ReportDataBase = ConfigurationManager.AppSettings["ReportDataBase"].ToString();
            //获取UserId
            ReportUserName = ConfigurationManager.AppSettings["ReportUserName"].ToString();
            //获取password
            ReportPassWord = ConfigurationManager.AppSettings["ReportPassWord"].ToString();
            //设置logOnInfo参数
            logOnInfo.ConnectionInfo.ServerName = ReportServerName;
            logOnInfo.ConnectionInfo.DatabaseName = ReportDataBase;
            logOnInfo.ConnectionInfo.UserID = ReportUserName;
            logOnInfo.ConnectionInfo.Password = ReportPassWord;

            oRpt.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
            oRpt.SetDataSource(ds);
            //CrystalReportViewer1.ParameterFieldInfo = paramFields;

            //建立.rpt文件与CryStalReportviewer文件之间的连接
            CrystalReportViewer1.ReportSource = oRpt;

            DataBind();

        }
    }
}
